package com.example.studentdemo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.studentdemo.common.Result;
import com.example.studentdemo.constant.RoleConstant;
import com.example.studentdemo.dto.LoginDTO;
import com.example.studentdemo.dto.RegisterDTO;
import com.example.studentdemo.entity.Account;
import com.example.studentdemo.entity.Student;
import com.example.studentdemo.entity.Teacher;
import com.example.studentdemo.exception.CustomerException;
import com.example.studentdemo.service.AdminService;
import com.example.studentdemo.service.StudentService;
import com.example.studentdemo.service.TeacherService;
import com.example.studentdemo.utils.PasswordUtils;

import com.example.studentdemo.utils.TokenUtils;
import com.example.studentdemo.vo.LoginVo;
import com.example.studentdemo.vo.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@Tag(name = "用户认证", description = "用户认证相关api")
public class AuthController {
    @Resource
    AdminService adminService;
    @Resource
    TeacherService teacherService;
    @Resource
    StudentService studentService;

    /**
     * 用户登录
     *
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口")
    @Parameter(description = "用户账号信息")
    public Result login(@RequestBody LoginDTO loginDTO) {
        log.info("用户登录：{}", loginDTO);
        Account dbAccount;
        Account account = new Account();
        BeanUtil.copyProperties(loginDTO, account);
        if ("ADMIN".equals(loginDTO.getRole())) {
            dbAccount = adminService.login(account);
        } else if ("TEACHER".equals(loginDTO.getRole())) {
            dbAccount = teacherService.login(account);
        } else if ("STUDENT".equals(loginDTO.getRole())) {
            dbAccount = studentService.login(account);
        } else {
            throw new CustomerException("非法请求");
        }
        LoginVo loginVo = LoginVo.builder().token(dbAccount.getToken()).build();
        return Result.success(loginVo);
    }

    /**
     * 用户注册
     *
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户注册接口")
    @Parameter(description = "用户账号信息")
    public Result register(@RequestBody RegisterDTO registerDTO) {
        log.info("用户注册：{}", registerDTO);
        if (RoleConstant.TEACHER_ROLE.equals(registerDTO.getRole())) {
            Teacher teacher = new Teacher();
            BeanUtil.copyProperties(registerDTO, teacher);
            // 密码加密
            teacher.setPassword(PasswordUtils.encryptPassword(registerDTO.getPassword()));
            teacherService.register(teacher);
        } else if (RoleConstant.STUDENT_ROLE.equals(registerDTO.getRole())) {
            Student student = new Student();
            BeanUtil.copyProperties(registerDTO, student);
            // 密码加密
            student.setPassword(PasswordUtils.encryptPassword(registerDTO.getPassword()));
            studentService.register(student);
        } else {
            throw new CustomerException("非法请求");
        }
        return Result.success();
    }

    @GetMapping("/userInfo")
    @Operation(summary = "获取用户信息", description = "获取当前登录用户的信息")
    public Result userInfo() {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null) {
            throw new CustomerException("当前用户不存在");
        }
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtil.copyProperties(currentUser, userInfoVo);
        return Result.success(userInfoVo);
    }
}
